package com.xsh.controller;


import com.xsh.pojo.User;
import com.xsh.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ContextLoader;

@Controller
@RequestMapping("/user")
public class UserController {

    private ApplicationContext context = ContextLoader.getCurrentWebApplicationContext();

    private UserService userService = context.getBean("userServiceImpl", UserService.class);

    @GetMapping("/loginPage")
    public String loginPage(){
        return "login";
    }

    @GetMapping("/insert")
    public String insert(){
        userService.insert(new User(0, "admin", "123456", ""));
        return "redirect:/user/loginPage";
    }

    @PostMapping("/login")
    public String login(User user){
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(user.getUsername(), user.getPassword()));
        return "WEB-INF/index";
    }
}
